package com.ww.springboot.boot.thread.study.base.lock.demo;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述：锁各个方法debug
 *
 * lock()没有抢到资源时就会进入同步队列中进行等待
 *
 * Node的理解:
 * SIGNAL -> 唤醒 标识符 将前置节点标识符设置为唤醒 当前节点就可以被阻塞 减少无用自旋的性能消耗
 *
 *
 * @author 🧑 ‍wanwei
 * @since 2022-08-05 17:45
 */
public class AqsDemo2 {


    /**
     * 创建三把锁 锁之间互不干扰
     */
    private static ReentrantLock lock = new ReentrantLock();


    public static void main(String[] args) {
        lock.lock();
        System.out.println(lock);
        lock.unlock();
        System.out.println(lock);
        System.out.println(lock.hasQueuedThreads());
        System.out.println(lock.isLocked());
        System.out.println(lock.tryLock());
        //重入锁 所以可以多次获取
        lock.lock();
        System.out.println(lock);
        lock.lock();
        System.out.println(lock);
        lock.lock();
        System.out.println(lock);

        for (int i = 0; i < 10; i++) {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            new Thread(()->{
                System.out.println("xxxxxxxxxxx");
                System.out.println(lock);
                lock.lock();
                System.out.println(lock);
            }).start();
        }
    }
}
